#include "BlockQueue.hpp"
#include <ctime>
#include <unistd.h>
#include <sys/types.h>

void *product(void *args)
{
    BlockQueue<int> *bq = static_cast<BlockQueue<int> *>(args);
    while (1)
    {
        int data = rand() % 10 + 1; // 数据范围1-10
        bq->push(data);
        std::cout << "生产者数据：" << data << std::endl;

        sleep(1);
    }
    return nullptr;
}

void *consume(void *args)
{
    BlockQueue<int> *bq = static_cast<BlockQueue<int> *>(args);
    while (1)
    {
        int data;
        bq->pop(&data);
        std::cout << "消费者数据：" << data << std::endl;

        // sleep(1);
    }

    return nullptr;
}

int main()
{
    srand((unsigned int)time(nullptr) ^ getpid());
    BlockQueue<int> *bq = new BlockQueue<int>;

    pthread_t c, p;
    pthread_create(&c, nullptr, consume, bq);
    pthread_create(&p, nullptr, product, bq);

    pthread_join(c, nullptr);
    pthread_join(p, nullptr);

    delete bq;
    return 0;
}